package com.tfswue.cxy.controller;

import com.tfswue.cxy.entity.Book;
import com.tfswue.cxy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    // 查询单本书
    @GetMapping("/{id}")
    public Book getBook(@PathVariable Long id) throws Exception {
        return bookService.getBookById(id);
    }

    // 查询所有图书
    @GetMapping
    public List<Book> getAllBooks() {
        return bookService.getAllBooks();
    }

    // 新增图书
    @PostMapping
    public String addBook(@RequestBody Book book) {
        bookService.addBook(book);
        return "success";
    }

    // 删除图书
    @DeleteMapping("/{id}")
    public String deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
        return "success";
    }

    // 修改图书
    @PutMapping
    public String updateBook(@RequestBody Book book) {
        bookService.updateBook(book);
        return "success";
    }
}